import {
  queryDefaultSet,
  addDefaultSet,
  removeDefaultSet,
  updateDefaultSet,
  queryTreeOrgList,
  queryOrgList,
  checkStrategyUser,
  addStrategyUser,
  queryServerType,
  queryDetail,
  cancelBindPerson,
} from '@/services/strategy/define';

export default {
  namespace: 'define',

  state: {
    // 列表数据
    data: {
      dataList: [],
      pageNum: 0,
      pageSize: 0,
      totalCount: 0,
    },
    dataDetail: {
      dataList: [],
      pageNum: 0,
      pageSize: 0,
      totalCount: 0,
    },
    queryForm: {},
  },

  effects: {
    *queryList({ payload }, { call, put }) {
      const response = yield call(queryDefaultSet, payload);
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *add({ payload, callback }, { call, put }) {
      const response = yield call(addDefaultSet, payload);
      yield put({
        type: 'saveAdd',
        payload: response,
      });
      if (callback) callback(response);
    },
    *remove({ payload, callback }, { call, put }) {
      const response = yield call(removeDefaultSet, payload);
      yield put({
        type: 'save',
        payload: response,
      });
      if (callback) callback(response);
    },
    *update({ payload, callback }, { call, put }) {
      const response = yield call(updateDefaultSet, payload);
      yield put({
        type: 'save',
        payload: response,
      });
      if (callback) callback(response);
    },
    *getTree({ payload, callback }, { call, put }) {
      const response = yield call(queryTreeOrgList, payload);
      yield put({
        type: 'saveTree',
        payload: response,
      });
      if (callback) callback(response);
    },
    *getList({ payload, callback }, { call, put }) {
      const response = yield call(queryOrgList, payload);
      yield put({
        type: 'saveList',
        payload: response,
      });
      if (callback) callback(response);
    },
    *bindUserRole({ payload, callback }, { call, put }) {
      const response = yield call(addStrategyUser, payload);
      yield put({
        type: 'saveList',
        payload: response,
      });
      if (callback) callback(response);
    },
    *checkRule({ payload, callback }, { call, put }) {
      const response = yield call(checkStrategyUser, payload);
      yield put({
        type: 'haha',
        payload: response,
      });
      if (callback) callback(response);
    },
    *serverType({ payload, callback }, { call, put }) {
      const response = yield call(queryServerType, payload);
      yield put({
        type: 'saveServerType',
        payload: response,
      });
      if (callback) callback(response);
    },
    *queryDetail({ payload }, { call, put }) {
      const response = yield call(queryDetail, payload);
      yield put({
        type: 'saveDetail',
        payload: response.data,
      });
    },

    *cancelBindPerson({ payload, callback }, { call, put }) {
      const response = yield call(cancelBindPerson, payload);
      yield put({
        type: 'saveCancel',
        payload: response,
      });
      if (callback) callback(response);
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload.data,
      };
    },
    saveDetail(state, action) {
      return {
        ...state,
        dataDetail: action.payload,
      };
    },
  },
};
